﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace TweetMemeNET
{
    /// <summary>
    /// Represents a single Twitter user
    /// </summary>
    public class User
    {
        /// <summary>
        /// Instantiates a new User object based on an XML string
        /// </summary>
        /// <param name="xml"></param>
        public User(string xml)
        {
            XElement e = XElement.Parse(xml);

            Populate(e);
        }

        /// <summary>
        /// Instantiates a new User object based on an XElement object
        /// </summary>
        /// <param name="tweetElement"></param>
        public User(XElement userElement)
        {
            Populate(userElement);
        }

        protected void Populate(XElement xml)
        {
            // Get XML elements
            XElement username=xml.Element(XName.Get("tweeter", string.Empty));
            XElement id=xml.Element(XName.Get("id", string.Empty));
            XElement location=xml.Element(XName.Get("location", string.Empty));
            XElement imageThumbnailUrl=xml.Element(XName.Get("thumb_url", string.Empty));
            XElement imageUrl=xml.Element(XName.Get("url", string.Empty));
            XElement name=xml.Element(XName.Get("ext_name", string.Empty));
            XElement bio=xml.Element(XName.Get("ext_bio", string.Empty));
            XElement externalUrl=xml.Element(XName.Get("ext_url", string.Empty));
            XElement followers=xml.Element(XName.Get("ext_follower_count", string.Empty));
            XElement following=xml.Element(XName.Get("ext_following_count", string.Empty));
            XElement locationExtended=xml.Element(XName.Get("ext_location", string.Empty));
            XElement created=xml.Element(XName.Get("ext_created_at", string.Empty));
            XElement score=xml.Element(XName.Get("score", string.Empty));
            XElement kudos=xml.Element(XName.Get("kudos", string.Empty));

            // Populate User properties based on XML elements
            Username = ParserHelper.GetElementValueOrEmptyString(username);
            ID=ParserHelper.GetElementValueOrEmptyString(id);
            Location=ParserHelper.GetElementValueOrEmptyString(location);
            ImageThumbnailUrl=ParserHelper.GetElementValueOrEmptyString(imageThumbnailUrl);
            ImageUrl=ParserHelper.GetElementValueOrEmptyString(imageUrl);
            Name=ParserHelper.GetElementValueOrEmptyString(name);
            Bio=ParserHelper.GetElementValueOrEmptyString(bio);
            ExternalUrl=ParserHelper.GetElementValueOrEmptyString(externalUrl);
            Followers=ParserHelper.GetElementIntValueOrZero(followers);
            Following=ParserHelper.GetElementIntValueOrZero(following);
            LocationExtended=ParserHelper.GetElementValueOrEmptyString(locationExtended);
            Created=ParserHelper.GetElementDateTimeValueOrMinValue(created);
            Score=ParserHelper.GetElementDoubleValueOrZero(score);
            Kudos=ParserHelper.GetElementDoubleValueOrZero(kudos);
        }

        /// <summary>
        /// Gets the ID of the comment
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// Gets the username of the user
        /// </summary>
        public string Username { get; set; }

        /// <summary>
        /// Gets the URL of the user's profile image
        /// </summary>
        public string ImageUrl { get; set; }

        /// <summary>
        /// Gets the URL of the user's profile image
        /// </summary>
        public string ImageThumbnailUrl { get; set; }

        /// <summary>
        /// Gets the location of the user's profile
        /// </summary>
        public string Location { get; set; }

        /// <summary>
        /// Gets the location of the user's profile
        /// </summary>
        public string LocationExtended { get; set; }

        /// <summary>
        /// Gets the extended (real) name of the Twitter user
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets the bio of the Twitter user
        /// </summary>
        public string Bio { get; set; }

        /// <summary>
        /// Gets the Twitter user's external website URL
        /// </summary>
        public string ExternalUrl { get; set; }

        /// <summary>
        /// Gets the number of followers the Twitter user has
        /// </summary>
        public int Followers { get; set; }

        /// <summary>
        /// Gets the number of people the Twitter user is following
        /// </summary>
        public int Following { get; set; }

        /// <summary>
        /// Gets the Twitter user's score
        /// </summary>
        public double Score { get; set; }

        /// <summary>
        /// Gets the Twitter user's kudos score
        /// </summary>
        public double Kudos { get; set; }

        /// <summary>
        /// Gets the create date of the Twitter account
        /// </summary>
        public DateTime Created { get; set; }

        /// <summary>
        /// Gets the time when the Twitter user profile was last updated
        /// </summary>
        public DateTime Updated { get; set; }
    }
}
